package com.certification.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.certification.common.utils.Constant;
import com.certification.dto.CertificationDTO;
import com.certification.entity.Certification;
import com.certification.service.CertificationService;
import com.certification.service.SysUserRoleService;
import com.certification.vo.CertificationVO;
import com.certification.vo.PageVO;
import com.certification.vo.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.certification.constants.CertConstant.CERTIFICATION;

/**
 * @author copa
 * @createDate 2022-03-28 21:55
 * @function 流程：认证申请
 */
@RestController
@Api(tags = {"流程：认证申请"})
public class CertificationController extends AbstractController{

    @Resource
    private CertificationService certificationService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @PostMapping("cert")
    @ApiOperation(value = "添加申请")
    public ResponseMessage<String> addCert(@RequestBody CertificationDTO certificationDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.addCert(certificationDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("certPage")
    @ApiOperation(value = "查看申请（分页）")
    public ResponseMessage<IPage<CertificationVO>> getCertPage(PageVO pageVO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.getCertPage(CERTIFICATION, Math.toIntExact(getUserId()), pageVO);
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("cert")
    @ApiOperation(value = "查看申请")
    public ResponseMessage<CertificationVO> getCert() {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.getCert(CERTIFICATION, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @PutMapping("cert")
    @ApiOperation(value = "编辑申请")
    public ResponseMessage<String> putCert(@RequestBody CertificationDTO certificationDTO) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.putCert(certificationDTO, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");

    }

    @GetMapping("cert/{certId}")
    @ApiOperation(value = "查看申请（详细）")
    public ResponseMessage<Certification> getCertDetail(@PathVariable Integer certId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.getCertDetail(certId, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("getCertById/{certId}")
    @ApiOperation(value = "查看申请（任何时候）")
    public ResponseMessage<Certification> getCertById(@PathVariable Integer certId) {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.getCertById(certId, Math.toIntExact(getUserId()));
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }

    @GetMapping("getCertById")
    @ApiOperation(value = "查看申请（任何时候）")
    public ResponseMessage<String> getCertById2() {
        for (Long roleId : sysUserRoleService.queryRoleIdList(getUser().getUserId())) {
            if (roleId == Constant.ROLE_PRINCIPAL) {
                return certificationService.getCertByIdByUserId(getUserId());
            }
        }
        return ResponseMessage.errorMsg("该角色不能调用该接口~");
    }


}
